package com.bigdata.demo.controller;

import com.bigdata.demo.methods.Connection_BigData;
import com.bigdata.demo.methods.Public;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController //用来表示这是一个对外的接口
//@CrossOrigin(originPatterns = "*", allowCredentials = "true", allowedHeaders = "*", methods = {})//跨域设置
//可以接受任何主机对本地的访问
@CrossOrigin("*")
public class Container_Delete {
    //这是大数据平台接口设计——容器删除——container_delete
    @RequestMapping("/docker/container_create/destory")
    public String create_docker(
            //接收到的参数
            @RequestParam("container_name") String container_name,  //容器名
            @RequestParam("container_namespace") String container_namespace  //容器所在命名空间
    ){

        System.out.println("del_container_name:"+container_name);
        System.out.println("del_container_namespace:"+container_namespace);

        String hostname = "125.221.232.243";//要连接主机的ip
        String username = "root";//登录的用户名
        String password = "123@bigdata";  //用户密码
        String pri; //接口的返回，运行状态

        //调用封装的类Connection_BigData，远程连接集群
        Connection_BigData con = new Connection_BigData(hostname, username, password);
        String path = Public.SHELL_dir;   //脚本所在目录
        String cmd="cd "+path+"; ";                   //找到脚本所在目录

        //拼接命令，例如：/bin/bash container_delete.sh nginx dev ，没使用container_volume
        String shell_name = "container_delete.sh ";             //脚本名
        //每个参数后面接空格，这里表示将这些参数传给脚本
        shell_name += container_name +' '+ container_namespace +' ';


        cmd += "/bin/bash "+shell_name;              //脚本执行的命令

        System.out.println("cmd:"+cmd);  //可以打印出来，看命令是否正确
        pri = con.connection(cmd); //调用函数connection，连接集群，并执行cmd命令，返回运行状态

        return pri;
        /********** 测 试  ************/
    }
}
